// Copyright 2021 Wechat Group, Tencent
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>
#include <iostream>
#include <limits>

#include "tfcc.h"

#include "environment.h"

class SignalTest : public testing::Test {
  static Environment _env;

 protected:
  static void SetUpTestCase() { _env.init(); }

  static void TearDownTestCase() { _env.release(); }

  static tfcc::DeviceType GetDeviceType() { return _env.getCurrentDeviceType(); }
};

Environment SignalTest::_env;

template <class T>
tfcc::Variable<T> complex_to_raw(const tfcc::Tensor<tfcc::Complex<T>>& value) {
  std::vector<unsigned> shape = value.shape().toVector();
  shape.push_back(2);
  std::vector<T> data;
  for (tfcc::Complex<T> v : tfcc::data::get(value)) {
    data.push_back(v.real);
    data.push_back(v.imag);
  }

  return tfcc::data::set(data, shape);
}

template <class T>
tfcc::Variable<tfcc::Complex<T>> raw_to_complex(const tfcc::Tensor<T>& tensor) {
  std::vector<unsigned> shape = tensor.shape().toVector();
  shape.pop_back();
  std::vector<tfcc::Complex<T>> data;
  std::vector<T> value = tfcc::data::get(tensor);
  for (size_t i = 0; i < value.size(); i += 2) {
    data.push_back({value[i], value[i + 1]});
  }

  return tfcc::data::set(data, shape);
}

#ifdef TFCC_USE_MKL

TEST_F(SignalTest, rfft) {
  if (SignalTest::GetDeviceType() == tfcc::DeviceType::CUDA) return;
  auto scope1 = tfcc::Scope::scope("signal");
  auto scope2 = tfcc::Scope::scope("rfft");
  size_t count = tfcc::Configure<uint64_t>::getConfigure("__count__");
  for (size_t i = 0; i < count; ++i) {
    auto scope3 = tfcc::Scope::scope(std::to_string(i));
    auto& a = tfcc::Constant<float>::getConstant("a");
    unsigned length = tfcc::Configure<unsigned>::getConfigure("length");
    auto& expect = tfcc::Constant<float>::getConstant("expect");
    auto result = tfcc::signal::rfft(a, length);
    ASSERT_TRUE(tfcc::is_similar(complex_to_raw(result), expect));
  }
}

TEST_F(SignalTest, irfft) {
  if (SignalTest::GetDeviceType() == tfcc::DeviceType::CUDA) return;
  auto scope1 = tfcc::Scope::scope("signal");
  auto scope2 = tfcc::Scope::scope("irfft");
  size_t count = tfcc::Configure<uint64_t>::getConfigure("__count__");
  for (size_t i = 0; i < count; ++i) {
    auto scope3 = tfcc::Scope::scope(std::to_string(i));
    auto& a = tfcc::Constant<float>::getConstant("a");
    unsigned length = tfcc::Configure<unsigned>::getConfigure("length");
    auto& expect = tfcc::Constant<float>::getConstant("expect");
    auto result = tfcc::signal::irfft(raw_to_complex(a), length);
    ASSERT_TRUE(tfcc::is_similar(result, expect));
  }
}

#endif
